#pragma once

#include "Reactor.hpp"
#include "Sock.hpp"
#include "Service.hpp"
#include "Util.hpp"

int Accepter(Event *evp)
{
    std::cout << "有新的链接到来了,就绪的sock是: " << evp->sock << std::endl;
    //ET模式，必须循环读取--->文件描述符必须设置为非阻塞
    while(true)
    {
        int sock = Sock::Accept(evp->sock);
        if(sock < 0)
        {
            std::cout << "Accept Done!" << std::endl;
            break;
        }
        std::cout << "Accept success: " << sock << std::endl;
        SetNonBlock(sock);
        //这里呢？获取链接成功,IO socket
        Event *other_ev = new Event();
        other_ev->sock = sock;
        other_ev->R = evp->R; //为什么要让所有的Event指向自己所属的Reactor?
        // 因为这样的话，就可以用Event的对象调用Reactor中的方法

        //recver, sender, errorer,就是我们代码中的较顶层，只负责真正的读取！
        other_ev->RegisterCallback(Recver, Sender, Errorer); //设置为正常读写

        evp->R->InsertEvent(other_ev, EPOLLIN|EPOLLET);
    }
}
